home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / lib / H / rules / prs2locks.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  10.8 KB  |  292 lines

  1. /*
  2.  * FILE:
  3.  *   prs2locks.h
  4.  *
  5.  * IDENTIFICATION:
  6.  *   $Header: /private/postgres/src/lib/H/rules/RCS/prs2locks.h,v 1.14 1992/03/11 02:20:38 mer Exp $
  7.  *
  8.  * DESCRIPTION:
  9.  *   include stuff for PRS2 locks.
  10.  *   This file contain only the definition for the prs2Locks, so that
  11.  *   it can be easily included in "htup.h".
  12.  *   For declarations of routines that manipulate locks, see
  13.  *   "prs2.h"
  14.  */
  15.  
  16. /*
  17.  * Include this file only once...
  18.  */
  19. #ifndef Prs2LocksIncluded
  20. #define Prs2LocksIncluded
  21. #define PRS2LOCKS_H
  22.  
  23. #include "tmp/postgres.h"
  24. #include "access/attnum.h"
  25.  
  26. /* -----------------------------------
  27.  * Comment out the following to suppress debugging output
  28.  *
  29.  * #define PRS2_DEBUG 1
  30.  */
  31.  
  32. /*==================================================================
  33.  * PRS2 LOCKS
  34.  *==================================================================
  35.  */
  36.  
  37. /*------------------------------------------------------------------
  38.  * event types: the types of event that can cause rule activations...
  39.  *------------------------------------------------------------------
  40.  */
  41. typedef char EventType;
  42. #define EventTypeRetrieve    'R'
  43. #define EventTypeReplace    'U'
  44. #define EventTypeAppend        'A'
  45. #define EventTypeDelete        'D'
  46. #define EventTypeInvalid    '*'
  47.  
  48. /*------------------------------------------------------------------
  49.  * Action types. These are the possible actions that can be defined
  50.  * in the 'do' part of a rule.
  51.  * Currently the actiosn suuported are:
  52.  * ActionTypeRetrieveValue: the action part of the rule is a
  53.  *   'retrieve (expression) where ....'. (a 'retrieve  into relation'
  54.  *   query is NOT of this type!
  55.  *   NOTE: under the current implementation, if such an action is
  56.  *   specified in the action part of a rule, no other action can
  57.  *   be specified!
  58.  * ActionTypeReplaceCurrent: the action is a 'replace CURRENT(x=..)'
  59.  *   NOTE: under the current implementation, if such an action is
  60.  *   specified in the action part of a rule, no other action can
  61.  *   be specified!
  62.  * ActionTypeReplaceNew: the action is a 'replace NEW(x=..)'
  63.  *   NOTE: under the current implementation, if such an action is
  64.  *   specified in the action part of a rule, no other action can
  65.  *   be specified!
  66.  * ActionTypeOther: Any other action... (including 'retrieve into...')
  67.  *------------------------------------------------------------------
  68.  */
  69. typedef char ActionType;
  70. #define ActionTypeRetrieveValue        'r'
  71. #define ActionTypeReplaceCurrent    'u'
  72. #define ActionTypeReplaceNew        'U'
  73. #define ActionTypeOther            'o'
  74. #define ActionTypeInvalid        '*'
  75.  
  76. /*------------------------------------------------------------------
  77.  * Plan numbers for various rule plans (as stored in the system catalogs)
  78.  *------------------------------------------------------------------
  79.  */
  80. typedef uint16 Prs2PlanNumber;
  81.  
  82. /* #define QualificationPlanNumber        ((Prs2PlanNumber)0) */
  83. #define ActionPlanNumber        ((Prs2PlanNumber)1)
  84.  
  85. /*------------------------------------------------------------------
  86.  * Used to distinguish between 'old' and 'new' tuples...
  87.  *------------------------------------------------------------------
  88.  */
  89. #define PRS2_OLD_TUPLE 1
  90. #define PRS2_NEW_TUPLE 2
  91.  
  92. /*------------------------------------------------------------------
  93.  * Types of locks..
  94.  *
  95.  * We distinguish between two types of rules: the 'backward' & 'forward'
  96.  * chaning rules. The 'backward' chaining rules are the ones that
  97.  * calculate a value for the current tuple, like:
  98.  *    on retrieve to EMP.salary where EMP.name = "mike"
  99.  *    do instead retrieve (salary=1000)
  100.  * or
  101.  *    on append to EMP where EMP.age<25
  102.  *    do replace CURRENT(salary=2000)
  103.  *
  104.  * The forward chaining rules are the ones that do some action but do not
  105.  * update the current tuple, like:
  106.  *    on replace to EMP.salary where EMP.name = "mike"
  107.  *    do replace EMP(salary=NEW.salary) where EMP.name = "john"
  108.  * or
  109.  *    on retrieve to EMP.age where EMP.name = "mike"
  110.  *    do append TEMP(username = user(), age = OLD.age)
  111.  *
  112.  * The first type of rules gets a `LockTypeXXXWrite' lock (where XXX is
  113.  * the event specified in the "on ..." clause and can be one of
  114.  * retrieve, append, delete or replace) and the forward chinign rules
  115.  * get locks of the form `LockTypeXXXAction'
  116.  *
  117.  * There is also the LockTypeRetrieveRelation, put by 'view' rules, 
  118.  * i.e. rules of the form
  119.  *    ON retrieve to relation
  120.  *      DO [ instead ] retrieve .... <some other tuples> ...
  121.  * These rules are used to implement views.
  122.  *
  123.  * NOTE: as we have 2 rule systems (tuple-level & query rewrite)
  124.  * we use different lock types for each one of them.
  125.  * Otherwise it wouldn't be possible to distinguish whether a rule
  126.  * is a tuple-level rule or of query-rewrite flavor.
  127.  *
  128.  *------------------------------------------------------------------
  129.  */
  130. typedef char Prs2LockType;
  131.  
  132. #define LockTypeInvalid            ((Prs2LockType) '*')
  133.  
  134. #ifdef OBSOLETE
  135. /*--- QUERY REWRITE LOCK TYPES ------------------------------------*/
  136. #define LockTypeTupleRetrieveAction        ((Prs2LockType) '1')
  137. #define LockTypeTupleAppendAction        ((Prs2LockType) '2')
  138. #define LockTypeTupleDeleteAction        ((Prs2LockType) '3')
  139. #define LockTypeTupleReplaceAction        ((Prs2LockType) '4')
  140. #define LockTypeTupleRetrieveWrite        ((Prs2LockType) '5')
  141. #define LockTypeTupleAppendWrite        ((Prs2LockType) '6')
  142. #define LockTypeTupleDeleteWrite        ((Prs2LockType) '7')
  143. #define LockTypeTupleReplaceWrite        ((Prs2LockType) '8')
  144. #define LockTypeTupleRetrieveRelation        ((Prs2LockType) '9')
  145. #endif OBSOLETE
  146.  
  147. /*--- TUPLE LEVEL LOCK TYPES --------------------------------------*/
  148. #define LockTypeRetrieveAction        ((Prs2LockType) 'r')
  149. #define LockTypeAppendAction        ((Prs2LockType) 'a')
  150. #define LockTypeDeleteAction        ((Prs2LockType) 'd')
  151. #define LockTypeReplaceAction        ((Prs2LockType) 'u')
  152. #define LockTypeRetrieveWrite        ((Prs2LockType) 'R')
  153. #define LockTypeAppendWrite        ((Prs2LockType) 'A')
  154. #define LockTypeDeleteWrite        ((Prs2LockType) 'D')
  155. #define LockTypeReplaceWrite        ((Prs2LockType) 'U')
  156. #define LockTypeRetrieveRelation    ((Prs2LockType) 'V')
  157. #define LockTypeImport            ((Prs2LockType) 'I')
  158. #define LockTypeExport            ((Prs2LockType) 'E')
  159.  
  160. /*------------------------------------------------------------------
  161.  * Every single lock (`Prs2OneLock') has the following fields:
  162.  *    ruleId:        OID of the rule
  163.  *    lockType:    the type of locks
  164.  *    attributeNumber:the attribute that is locked. A value
  165.  *            equal to 'InvalidAttributeNumber' means that
  166.  *                      all the attributes of the tuple are locked.
  167.  *    planNumber:    the plan number of the plan (which is stored
  168.  *            in the system catalogs) that must be executed
  169.  *                      if the rule is activated. This is usually
  170.  *                      equal to 'ActionPlanNumber', but we might add
  171.  *                      others as well when we'll implement a 
  172.  *                      more complex locking scheme.
  173.  *    partialindx,
  174.  *    npartial:    This is a field unique to 'import' locks.
  175.  *            A tuple might have many "import" locks,
  176.  *            depending on the rule. Each such import
  177.  *            lock is only a "partial" lock, and in order
  178.  *            for the tuple to be locked, we must have all
  179.  *            its partial locks. `npartial' is the number
  180.  *            of locks this tuple must have to be considered
  181.  *            as locked, and foreach lock `partialindx'
  182.  *            is the "index" of this partial lock
  183.  *            (a number between 0 ... npartial-1).
  184.  *            NOTE: we might have more than one locks with
  185.  *            the same `partialindx'. However they only count
  186.  *            as one.
  187.  *
  188.  * Every tuple can have more than one rule locks, so we need
  189.  * a structure 'Prs2LocksData' to hold them.
  190.  *       numberOfLocks:    the number of entries in the 'locks' array.
  191.  *    locks[]:    A *VARIABLE LENGTH* array with 0 or more
  192.  *                      entries of type Prs2OneLock.
  193.  *------------------------------------------------------------------
  194.  */
  195. typedef struct Prs2OneLockData {
  196.     ObjectId        ruleId;    
  197.     Prs2LockType    lockType;
  198.     AttributeNumber    attributeNumber;
  199.     Prs2PlanNumber    planNumber;
  200.     int            partialindx;
  201.     int            npartial;
  202. } Prs2OneLockData;
  203.  
  204. typedef Prs2OneLockData *Prs2OneLock;
  205.  
  206. typedef struct Prs2LocksData {
  207.     int            numberOfLocks;
  208.     Prs2OneLockData    locks[1];    /* XXX VARIABLE LENGTH DATA */
  209. } Prs2LocksData;
  210.  
  211. typedef Prs2LocksData    *RuleLock;
  212.  
  213.  
  214. /*------------------------------------------------------------------
  215.  * INVALID RULE LOCK:
  216.  *
  217.  * This is an illegal value for a (main memory representation) of a
  218.  * rule lock. You must not confuse it with the "empty" lock, i.e.
  219.  * the lock: "(numOfLocs: 0)"::lock.
  220.  *
  221.  * However note that (as even an empty rule lock is 4 bytes long)
  222.  * in order to save disk space, when we 
  223.  * store an empty lock in the disk, we use an InvalidItemPointer
  224.  *
  225.  * NOTE: `RuleLockIsValid' only applies for the main memory
  226.  * representation of a lock!
  227.  */
  228. #define InvalidRuleLock        ((RuleLock) NULL)
  229. #define RuleLockIsValid(x)    PointerIsValid(x)
  230.  
  231. /*------------------------------------------------------------------
  232.  * #defines to access/set Prs2Lock data...
  233.  * It is highly recommended that these routines are used instead
  234.  * of directly manipulating the various structures invloved..
  235.  *------------------------------------------------------------------
  236.  */
  237.  
  238. #define prs2OneLockGetRuleId(l)            ((l)->ruleId)
  239. #define prs2OneLockGetLockType(l)        ((l)->lockType)
  240. #define prs2OneLockGetAttributeNumber(l)    ((l)->attributeNumber)
  241. #define prs2OneLockGetPlanNumber(l)        ((l)->planNumber)
  242. #define prs2OneLockGetPartialIndx(l)        ((l)->partialindx)
  243. #define prs2OneLockGetNPartial(l)        ((l)->npartial)
  244.  
  245. #define prs2OneLockSetRuleId(l, x)        ((l)->ruleId = (x))
  246. #define prs2OneLockSetLockType(l, x)        ((l)->lockType = (x))
  247. #define prs2OneLockSetAttributeNumber(l, x)    ((l)->attributeNumber = (x))
  248. #define prs2OneLockSetPlanNumber(l, x)        ((l)->planNumber = (x))
  249. #define prs2OneLockSetPartialIndx(l, x)        ((l)->partialindx = (x))
  250. #define prs2OneLockSetNPartial(l, x)        ((l)->npartial = (x))
  251.  
  252. /*------------------------------------------------------------------
  253.  * prs2LockSize
  254.  *    return the size needed for a 'Prs2LocksData' structure big enough
  255.  *    to hold 'n' of 'Prs2OneLockData' structures...
  256.  */
  257. #define prs2LockSize(n) (sizeof(Prs2LocksData) \
  258.             + ((n)-1)*sizeof(Prs2OneLockData))
  259.  
  260. /*------------------------------------------------------------------
  261.  * prs2GetNumberOfLocks
  262.  *    return the number of locks contained in a 'RuleLock' structure.
  263.  */
  264. #define prs2GetNumberOfLocks(x)    ((x)==NULL ? 0 : (x)->numberOfLocks)
  265.  
  266. /*------------------------------------------------------------------
  267.  * prs2RuleLockIsEmpty
  268.  *     return true if this is an empty rule lock.
  269.  */
  270. #define prs2RuleLockIsEmpty(l)    (prs2GetNumberOfLocks(l) == 0)
  271.  
  272. /*=================================================================
  273.  *
  274.  * VARIOUS SYSTEM RELATION CONSTANTS
  275.  *
  276.  *=================================================================
  277.  */
  278.  
  279. /*----
  280.  * pg_type.oid for the "lock" type
  281.  */
  282. #define PRS2_LOCK_TYPEID    ((ObjectId) 31)
  283. #define PRS2_BOOL_TYPEID    ((ObjectId) 16)
  284. /*---
  285.  * varnos for the NEW & CURRENT tuple.
  286.  * these are hardwired in the parser code...
  287.  */
  288. #define PRS2_CURRENT_VARNO    1
  289. #define PRS2_NEW_VARNO        2
  290.  
  291. #endif Prs2LocksIncluded
  292.